package com.sp.api.springbootapi.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by weidewang on 2018/7/10
 * 告警系统生产者
 */
public class AlertWarningProducer {
    public static final String EXCHANGE_NAME="alerts";
    public static final String ROUTING_KEY="critical.alert";

    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;

        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            factory.setUsername("guest");
            factory.setPassword("guest");
            factory.setPort(5672);
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare(EXCHANGE_NAME,"topic",true);

            String message = "critical content!!!";

            channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY,null,message.getBytes());

            System.out.println(" [x] Sent '" + ROUTING_KEY + "':'" + message + "'");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                }
                catch (Exception ignore) {}
            }
        }
    }
}
